#include "fighter_alex.hpp"

using namespace Shiny;
using namespace Shiny::Applejack;

FighterAlex::FighterAlex(World& w, Body& b, Applejack::View* camera) : Player(w, b, camera)
{
    _damageResistance = 60;
    _speed            = 100;
    _strength         = 7;

    _isSmoking        = false;

    Fixture* fistLeft  = b.Fixture("fistLeft");
    Fixture* fistRight = b.Fixture("fist");

    _attackPatateLeft  = new Attack(_doingPatateLeft, fistLeft);
    _attackPatateRight = new Attack(_doingPatateRight, fistRight);

    _attackPatateLeft->damage  = 30;
    _attackPatateRight->damage = 30;
    _attackPatateLeft->effects.push_back(Attack::Effect(Attack::Effect::STUN, 5000.f, 0.f));
    _attackPatateRight->effects.push_back(Attack::Effect(Attack::Effect::STUN, 5000.f, 0.f));

    _attacks.push_back(_attackPatateLeft);
    _attacks.push_back(_attackPatateRight);

    Shiny::TextureList* tex = _world.Hud()->GetTextureList();

    if (tex)
    {
	AddSpellDisplay(spell1, 0, (*tex)["spell-spliff"]);
	AddSpellDisplay(spell2, 1, (*tex)["spell-insult"]);
	AddSpellDisplay(spell3, 2, (*tex)["spell-gringalet"]);
	AddSpellDisplay(spell4, 3, (*tex)["spell-patate"]);
    }

    Shiny::SoundList& sounds  = _world.GetSounds();
    const sf::SoundBuffer*  victory = sounds["louis-victory"];

    if (victory)      _soundVictory.SetBuffer(*victory);
}

void FighterAlex::Victory(bool hasWon)
{
    if (hasWon && _soundVictory.GetBuffer())
	SoundManager::Get()->Play(&_soundVictory);
}

void FighterAlex::GetKicked(int damage, const Attack::Effects& effects)
{
  Player::GetKicked(damage, effects);
}

void FighterAlex::SpellFirst(sf::Event)
{
    if (!_isSmoking && _cooldownSpliff.GetElapsedTime().AsSeconds() > CooldownSpliff)
    {
	_damageResistanceModifier += 50;
	_isSmoking = true;
    }
}

void FighterAlex::SpellSecond(sf::Event)
{
}

void FighterAlex::SpellThird(sf::Event)
{
}

void FighterAlex::SpellUltimate(sf::Event)
{
    if (!_doingPatateLeft && !_doingPatateRight && _cooldownPatate.GetElapsedTime().AsSeconds() > CooldownPatate)
    {
	if (_lookingLeft)
	  _body.Sprite().SetAnimation(12);
	else
	  _body.Sprite().SetAnimation(11);
	_observerPatateAnim = _body.Sprite().AnimationFinished.Connect(*this, &FighterAlex::EndPatate);
	_cooldownPatate.Restart();
    }
}

void FighterAlex::EndPatate(unsigned int anim)
{
    if (anim == 12)
	_doingPatateLeft  = true;
    else if (anim == 11)
	_doingPatateRight = true;
    _finishingPatate.Restart();
    _body.Sprite().AnimationFinished.Disconnect(_observerPatateAnim);
}

void FighterAlex::Run(void)
{
    if ((_doingPatateLeft || _doingPatateRight) && _finishingPatate.GetElapsedTime().AsMilliseconds() > 150.f)
    {
	if (_doingPatateLeft)
	{
	    _doingPatateLeft = false;
	    _body.Sprite().SetAnimation(2);
	}
	if (_doingPatateRight)
	{
	    _doingPatateRight = false;
	    _body.Sprite().SetAnimation(0);
	}
    }
    DisplaySpells();
    Player::Run();
}

void FighterAlex::DisplaySpells()
{
    UpdateSpellProgressBar(_cooldownSpliff.GetElapsedTime().AsSeconds(), CooldownSpliff, spell1);
    UpdateSpellProgressBar(_cooldownInsult.GetElapsedTime().AsSeconds(), CooldownInsult, spell2);
    UpdateSpellProgressBar(_cooldownLongboard.GetElapsedTime().AsSeconds(), CooldownLongboard, spell3);
    UpdateSpellProgressBar(_cooldownPatate.GetElapsedTime().AsSeconds(), CooldownPatate, spell4);
}
